package com.google.coffeeshop.business;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import vn.bang.utility.JpaUtil;

import com.google.coffeeshop.dao.DAOFactory;
import com.google.coffeeshop.dao.SeatDAO;
import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.entity.ProductGroupDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.log.Log;

public class LoadingBusiness extends AbstractBaseBusiness {

	static public List<SeatDTO> getSeatsDefault() {
		List<SeatDTO> seats = new ArrayList<SeatDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			SeatDAO seatDAO = DAOFactory.createSeatDAO(entityManager);
			seats.addAll(seatDAO.findWithoutNewAndTerminatedStatus());
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return seats;
	}
	
	static public List<ProductDTO> getAllProducts() {
		List<ProductDTO> product = new ArrayList<ProductDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			product.addAll(DAOFactory.createProductDAO(entityManager).findAll());
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return product;
	}
	static public List<ProductGroupDTO> getAllProductGroup() {
		List<ProductGroupDTO> groups = new ArrayList<ProductGroupDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			groups.addAll(DAOFactory.createProductGroupDAO(entityManager).findAll());
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return groups;
	}
	
	static public List<ProductDTO> getActiveByGroupId(long groupId) {
		List<ProductDTO> products = new ArrayList<ProductDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			products.addAll(DAOFactory.createProductDAO(entityManager).findActiveByGroupId(groupId));
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return products;
	}
	
	
	static public List<SeatDTO> getAllSeats() {
		List<SeatDTO> seats = new ArrayList<SeatDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			seats.addAll(DAOFactory.createSeatDAO(entityManager).findAll());
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return seats;
	}
}
